package com.zhengx.blelib;

import android.bluetooth.BluetoothDevice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.zhengx.blehelper.BleCallback;
import com.zhengx.blehelper.BleManager;
import com.zhengx.blehelper.BluetoothLeClient;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    private LineRecordTask lineRecordTask;

    private BluetoothDevice bluetoothDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lineRecordTask = LineRecordTask.getInstance(this);


        lineRecordTask.setOnConnectCallback(new LineRecordTask.OnConnectCallback() {
            @Override
            public void foundDevices(List<BluetoothDevice> bluetoothDevices) {
                if (bluetoothDevices.size() >= 1) {
                    bluetoothDevice = bluetoothDevices.get(0);
                }
            }
        });

        Button btnScan = (Button) findViewById(R.id.scan);
        Button cancelScan = (Button) findViewById(R.id.cancelScan);

        Button btnConnect = (Button) findViewById(R.id.connectFirst);

        Button btnSend = (Button) findViewById(R.id.sendMessage);

        btnScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineRecordTask.scanDevices();
            }
        });

        cancelScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineRecordTask.getBleManager().cancelScan();
            }
        });


        btnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineRecordTask.connectDevice(bluetoothDevice);
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (BluetoothLeClient bluetoothLeClient : BleManager.getInstance().getClients()) {
                    lineRecordTask.sendCommand(bluetoothLeClient,
                            200, "cs\r\n".getBytes(), null);
                }

            }
        });
    }
}
